package demo3;
//八、抽象类和接口
/*
抽象类 ：
1.抽象类被继承，子类必须重写抽象类的抽象方法
2.抽象类不可以实例化自己，但可以实例化子类。
 */
/*
接口：
1.接口必须由类来实现
2.接口中的方法都为抽象方法
3.接口中的类必须都被实现的类给 重写
4.常量默认被 public static final修饰
5.被defalut、static修饰的方法可以被具体实现
 */



/*
请实现笔记本电脑使用USB鼠标、USB键盘的例子
1. USB接口：包含打开设备、关闭设备功能
2. 笔记本类：包含开机功能、关机功能、使用USB设备功能
3. 鼠标类：实现USB接口，并具备点击功能
4. 键盘类：实现USB接口，并具备输入功能
 */
interface USB{
    void openDevice();
    void closeDecive();
}
class Mouse implements USB{
    @Override
    public void openDevice() {
        System.out.println("鼠标打开设备");
    }

    @Override
    public void closeDecive() {
        System.out.println("鼠标关闭设备");
    }
    public void click(){
        System.out.println("鼠标点击功能");
    }
}
class KeyBord implements USB{
    @Override
    public void openDevice() {
        System.out.println("键盘打开设备");
    }

    @Override
    public void closeDecive() {
        System.out.println("键盘关闭设备");
    }
    public void input(){
        System.out.println("键盘输入功能");
    }
}
//2. 笔记本类：包含开机功能、关机功能、使用USB设备功能
class Computer {
    public void open(){
        System.out.println("开机");
    }
    public void close(){
        System.out.println("关机");
    }
    public void useUsbDevice(USB usb){
        usb.openDevice();
        if(usb instanceof Mouse){
            Mouse mouse=(Mouse)usb;
            mouse.click();
        }else if(usb instanceof KeyBord){
            KeyBord keyBord=(KeyBord)usb;
            keyBord.input();
        }
        usb.closeDecive();
    }

}
public class Test7 {
    public static void main1(String[] args) {
        Computer computer=new Computer();
        computer.open();
        computer.useUsbDevice(new Mouse());
        computer.useUsbDevice(new KeyBord());
        computer.close();

    }

    public static void main2(String[] args) {
        Duck duck=new Duck("唐老鸭",10);
        Bird bird=new Bird("小鸟",11);
        Pig pig=new Pig("小猪佩奇",8);
        eat(duck);
        eat(bird);
        eat(pig);
        System.out.println("====================");
        run(duck);
        run(pig);
        //run(bird);Bird并没有实现IRunning接口，所以不能调用传入IRunning参数的方法。
        System.out.println("====================");
        fly(duck);
        fly(bird);


    }
    public static void eat(Animals animals){
        animals.eat();
    }

    public static void run(IRunning iRunning){
        iRunning.running();
    }

    public static void swim(ISwimming iSwimming){
        iSwimming.swimming();
    }
    public static void fly(IFlying iFlying){
        iFlying.flying();
    }
}
interface ISwimming{
    void swimming();
}
interface IRunning{
    void running();
}
interface IFlying{
    void flying();
}
class Animals{
    public String name;
    public int age;
    public Animals(String name,int age){
        this.name=name;
        this.age=age;
    }
    public void eat(){
        System.out.println("正在吃饭");
    }
}

class Bird extends Animals implements IFlying{
   public Bird(String name,int age){
       super(name, age);
   }

    @Override
    public void eat() {
        System.out.println(this.name+"正在吃鸟粮");
    }

    @Override
    public void flying() {
        System.out.println(this.name+"正在飞");
    }
}
class Duck extends Animals implements IFlying,IRunning,ISwimming{
    public Duck(String name,int age){
        super(name,age);
    }
    @Override
    public void eat() {
        System.out.println(this.name+"正在吃鸭粮");
    }

    @Override
    public void flying() {
        System.out.println(this.name+"正在飞");
    }

    @Override
    public void running() {
        System.out.println(this.name+"正在跑");
    }

    @Override
    public void swimming() {
        System.out.println(this.name+"正在游泳");
    }
}
class Pig extends Animals implements IRunning,ISwimming{
    public Pig(String name,int age){
        super(name, age);
    }

    @Override
    public void running() {
        System.out.println(this.name+"正在跑");
    }

    @Override
    public void swimming() {
        System.out.println(this.name+"正在游泳");
    }

    @Override
    public void eat() {
        System.out.println(this.name+"正在吃猪粮");
    }
}


class Students implements Comparable<Students>{
    public String name;
    public int age;
    public Students(String name,int age){
        this.name=name;
        this.age=age;
    }
    @Override
    public int compareTo(Students o) {
        //return this.age-o.age;

        return this.name.compareTo(o.name);
        /*
        name是 String 类型的，无法直接计算；
        为什么this.name.compareTo(o.name)中还要调用一遍compareTo呢？
        答：主要记忆一下就可以；
        因为在String源代码中有实现comparable接口，所以也实现了compareTo方法
         */
    }
}
class TestStudents{
    public static void main(String[] args) {
       Students students1=new Students("hanmeimei",10);
       Students students2=new Students("lilei",11);
       System.out.println(students1.compareTo(students2));//谁调用的compareTo方法，谁就是this。剩下的那个是o。
    }
}